﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;
using Supersense.db;

public partial class header : System.Web.UI.UserControl
{
    public DataTable dt;
    public String channel;
    public DataTable[] hArray;
    public DataTable solutions;
    public DataTable industry;
    public TreeView treeView;

    protected void Page_Load(object sender, EventArgs e)
    {
        channel = System.IO.Path.GetFileName(Request.Path).ToString();
        string[] sArray = channel.Split('.');
        channel = sArray[0];
        Crud modelCategory = new Crud("ss_category","id");
        dt = modelCategory.fList("type='product' and upid=0", "", "", "");
        dt.Columns.Add("subCategory");

        this.industry = modelCategory.fList("type='industry'", "", "", "");

        DataTable subCategory = modelCategory.fList("type='product' and upid!=0", "", "", "");
        DataTable[] htArray = new DataTable[dt.Rows.Count];
        hArray = htArray;
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            htArray[i] = this.dealTable(subCategory, "upid", dt.Rows[i]["id"].ToString().Trim());
            //dt.Rows[i]["subCategory"] = this.dealTable(subCategory, "upid", dt.Rows[i].ToString());
        }

        Crud crudSolution = new Crud("ss_spacenews", "id");
        dealProduct(dt);


        this.subProduct(dt);
        this.solutions = crudSolution.fList("type='solution'","","","subject,id");


    }
    public DataTable dealTable(DataTable sourceTable, String key, String value)
    {
        DataTable ht = new DataTable();
        ht.Columns.Add("name");
        ht.Columns.Add("id");
        foreach (DataRow dr in sourceTable.Rows)
        {
            if (dr[key].ToString().Trim() == value)
            {
                DataRow row = ht.NewRow();
                row["name"] = dr["name"].ToString();
                row["id"] = dr["id"].ToString();
                ht.Rows.Add(row);
            }
        }
        return ht;
    }

    public DataSet dealProduct(DataTable dt)
    {
        Crud modelProduct = new Crud("ss_product","id");
        Crud modelCategory = new Crud("ss_category", "id");
        DataSet ds = new DataSet();
        DataTable products = modelProduct.fList("", "", "", "");
        foreach (DataRow dr in dt.Rows)
        {
            DataTable tmpdt = modelCategory.fList("type='product' and upid=" + dr["id"].ToString(), "", "", "");
            if (tmpdt == null)
            {
                DataTable tmpProDt = modelProduct.fList("type=" + dr["id"].ToString(), "", "", "");
                DataTable tmp = ds.Tables.Add(dr["id"].ToString());
                tmp = tmpProDt.Copy();
            }
        }
        return ds;
    }

    public void deal(DataTable dt)
    {
        Crud modelProduct = new Crud("ss_product", "id");
        Crud modelCategory = new Crud("ss_category", "id");
        DataSet ds = new DataSet();
        DataTable products = modelProduct.fList("", "", "", "");
        foreach (DataRow dr in dt.Rows)
        {
            DataTable tmpdt = modelCategory.fList("type='product' and upid=" + dr["id"].ToString(), "", "", "");
            Response.Write(tmpdt.Rows[0]["id"]);
            if (tmpdt == null)
            {
                DataTable tmpProDt = modelProduct.fList("type=" + dr["id"].ToString(), "", "", "");
                DataTable tmp = ds.Tables.Add(dr["id"].ToString());
                tmp = tmpProDt.Copy();
            }
        }


    }

    public void subProduct(DataTable dt) 
    {
        Crud modelProduct = new Crud("ss_product", "id");
        Crud modelCategory = new Crud("ss_category", "id");
        treeView = new TreeView();
        foreach (DataRow row in dt.Rows)
        {
            TreeNode tn = new TreeNode();
            tn.Value = row["id"].ToString();
            tn.Text = row["name"].ToString();
            DataTable tmpDt = modelCategory.fList("type='product' and upid=" + row["id"].ToString(), "", "", "");
            if (tmpDt != null)
            {
                foreach (DataRow dr in tmpDt.Rows)
                {
                    TreeNode tmpNode = new TreeNode();
                    tmpNode.Text = dr["name"].ToString();
                    tmpNode.Value = dr["id"].ToString();
                    tmpNode.Target = "cat";
                    DataTable productDt = modelProduct.fList("catid=" + dr["id"].ToString(), "", "", "");
                    if (productDt != null)
                    {
                        foreach (DataRow tmpDr in productDt.Rows)
                        {
                            TreeNode tmpTreeNode = new TreeNode();
                            tmpTreeNode.Text = tmpDr["name"].ToString();
                            tmpTreeNode.Value = tmpDr["id"].ToString();
                            tmpTreeNode.Target = "product";
                            tmpNode.ChildNodes.Add(tmpTreeNode);
                        }
                    }

                    tn.ChildNodes.Add(tmpNode);
                }
            }
            else
            {
                DataTable productDt1 = modelProduct.fList("type=" + row["id"].ToString(), "", "", "");
                if (productDt1 != null)
                {
                    foreach (DataRow tmpDr in productDt1.Rows)
                    {
                        TreeNode tmpTreeNode = new TreeNode();
                        tmpTreeNode.Text = tmpDr["name"].ToString();
                        tmpTreeNode.Value = tmpDr["id"].ToString();
                        tmpTreeNode.Target = "product";
                        tn.ChildNodes.Add(tmpTreeNode);
                    }
                }
            }
            treeView.Nodes.Add(tn);
        }

        //foreach (TreeNode tmpTn in treeView.Nodes)
        //{
        //    Response.Write(tmpTn.Text + "<br/>");
        //    if (tmpTn.ChildNodes.Count > 0)
        //    {
        //        foreach (TreeNode tmpTn1 in tmpTn.ChildNodes)
        //        {
        //            Response.Write("→" + tmpTn1.Text + "<br/>");
        //            if (tmpTn1.ChildNodes.Count > 0)
        //            {
        //                foreach (TreeNode tmpTr in tmpTn1.ChildNodes)
        //                {
        //                    Response.Write("→→" + tmpTr.Text + "<br/>");
        //                }
        //            }
        //        }
        //    }

        //}
    }




}